﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

using Entities.DB;
using Interfaces;

namespace WebZAH
{
    public partial class WebForm2 : System.Web.UI.Page
    {

        DataTable dt = new DataTable();
        IProductDBManager pdbm = new ProductDBManager();

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void HomeButton1_Click1(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("ProfileMain.aspx");
        }

        protected void RegistrationButton_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("UserRegistration.aspx");
        }

        protected void AboutButton_Click(object sender, ImageClickEventArgs e)
        {

        }

        protected void ProductsButton_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("Products.aspx");
        }

        protected void AccountButton_Click(object sender, ImageClickEventArgs e)
        {

        }

        protected void CartButton_Click(object sender, ImageClickEventArgs e)
        {

        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string category = DropDownList1.SelectedValue;

            if (category == "Please choose") ProductList.Visible = false;
            else FillListByCategory(category);
        }

        private void FillListByCategory(string _Category)
        {
            dt.Columns.Add("Name", System.Type.GetType("System.String"));
            dt.Columns.Add("Stock", System.Type.GetType("System.String"));
            dt.Columns.Add("Price", System.Type.GetType("System.String"));

            try
            {
                DataRow dr = dt.NewRow();

                IList<IProducts> productList = pdbm.GetProductByCategory(_Category);

                for (int i = 0; i < productList.Count; i++)
                {
                    dr = dt.NewRow();
                    dr["Name"] = productList[i].ProductName;
                    dr["Stock"] = productList[i].CurrentStock + " " + productList[i].StockUnits;
                    dr["Price"] = productList[i].Price + " BGN";
                    dt.Rows.Add(dr);
                }

                Session["TaskTable"] = dt;

                ProductList.DataSource = Session["TaskTable"];
                ProductList.DataBind();
                ProductList.Visible = true;
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }

        protected void ProductList_Sorting(object sender, GridViewSortEventArgs e)
        {

            //Retrieve the table from the session object.
            DataTable dt = Session["TaskTable"] as DataTable;

            if (dt != null)
            {

                //Sort the data.
                dt.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortExpression);
                ProductList.DataSource = Session["TaskTable"];
                ProductList.DataBind();
            }

        }

        private string GetSortDirection(string column)
        {

            // By default, set the sort direction to ascending.
            string sortDirection = "ASC";

            // Retrieve the last column that was sorted.
            string sortExpression = ViewState["SortExpression"] as string;

            if (sortExpression != null)
            {
                // Check if the same column is being sorted.
                // Otherwise, the default value can be returned.
                if (sortExpression == column)
                {
                    string lastDirection = ViewState["SortDirection"] as string;
                    if ((lastDirection != null) && (lastDirection == "ASC"))
                    {
                        sortDirection = "DESC";
                    }
                }
            }

            // Save new values in ViewState.
            ViewState["SortDirection"] = sortDirection;
            ViewState["SortExpression"] = column;

            return sortDirection;
        }

    }
}